/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.TransaksiParkirdanLaporan.Com.Transaksi;

import Login.MainForm;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Ardha
 */
public class TransaksiParkirController {

    private static javax.swing.JFrame frame;

    public static javax.swing.JFrame getFrame() {
        return frame;
    }

    public static void openLayananMasuk(javax.swing.JFrame frame, String jamMasukKaryawan) {
        TransaksiParkirController.frame = frame;
        frame.setVisible(false);
        new LayananMasukForm(jamMasukKaryawan).setVisible(true);
    }

    public static void openMainForm(javax.swing.JFrame frame) {
//        TransaksiParkirController.frame=frame;
        frame.setVisible(false);
//        TransaksiParkirController.frame.setVisible(true);

        LayananMasukForm layananMasuk = (LayananMasukForm) frame;
        MainForm mainForm = new MainForm(layananMasuk.jamMasukKaryawan);
        mainForm.setVisible(true);
    }

    public static void openMainForm2(javax.swing.JFrame frame) {
//        TransaksiParkirController.frame=frame;
        frame.setVisible(false);
//        TransaksiParkirController.frame.setVisible(true);

        LayananKeluarForm layananKeluar = (LayananKeluarForm) frame;
        MainForm mainForm = new MainForm(layananKeluar.jamMasuk);
        mainForm.setVisible(true);
    }

    public static void openLayananKeluar(javax.swing.JFrame frame, String jamMasukKaryawan) {
        TransaksiParkirController.frame = frame;
        frame.setVisible(false);
        new LayananKeluarForm(jamMasukKaryawan).setVisible(true);
    }

    public static void openRekap(javax.swing.JFrame frame, String x) throws SQLException {
        //TransaksiParkirController.frame = frame;
        frame.setVisible(false);
        try {
            new RekapForm(x).setVisible(true);
        } catch (IOException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void cetakLaporan(DataParkir dataParkir) {
        try {
            String reportSource = "./reports/LaporanBulanan.jasper";
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("no_Parkir", dataParkir.getNoParkir());
            params.put("no_Polisi_Kendaraan", dataParkir.getNoPolisiKendaraan());
            params.put("tanggal_Masuk", dataParkir.getTanggalMasuk());
            params.put("jam_Masuk", dataParkir.getJamMasuk());
            params.put("staff_Parkir", dataParkir.getIdKaryawan());
            params.put("jenis_Helm", dataParkir.getKode_merkHelm_jenisHelm());
            params.put("jumlah_Helm", String.valueOf(dataParkir.getJumlahHelm()));

            JasperPrint jasperPrint = JasperFillManager.fillReport("./reports/struk-Parkir.jasper", params);
            JasperViewer.viewReport(jasperPrint, false);
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

    public static void noParkir(JTextField jtextfield) {
        try {
            jtextfield.setText(DataParkir.getConnection().idParkir().substring(0, 5));
        } catch (SQLException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void hitungTotalRekap(JTable table, JLabel jumKendLabel, JLabel jumPendLabel) {
        int x = table.getRowCount();
        jumKendLabel.setText(String.valueOf(x));
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        long money = x * 2000;
        jumPendLabel.setText(nf.format(money));
    }

    public static void noRekap(JLabel jlabel) {
        try {
            jlabel.setText(DataRekap.getConnection().idRekap().substring(0, 7));
        } catch (SQLException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TransaksiParkirController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void cariDataParkir(DataParkir dp, JTextField tf1, JTextField tf2, JTextField tf3, JTextField pembayaran, JTextField np1, JTextField np2, JTextField np3) {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyy, hh:mm:ss");

            if (DataParkir.getConnection().cariDataParkirKeluar(dp) == true) {
                tf1.setText(dp.getKode_merkHelm_jenisHelm());
                tf2.setText(dp.getTanggalMasuk() + ", " + dp.getJamMasuk());
                tf3.setText(sdf.format(gc.getTime()));
                np1.setEditable(false);
                np2.setEditable(false);
                np3.setEditable(false);
                pembayaran.setEditable(true);
                pembayaran.requestFocus();

            } else {
                JOptionPane.showMessageDialog(null, "No Polisi kendaraan tidak terdaftar");
            }
        } catch (SQLException ex) {
            Logger.getLogger(LayananKeluarForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(LayananKeluarForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void parkirKeluar(DataParkir dp, JFrame frame, JButton button, JTextField field) {
        try {
            if (DataParkir.getConnection().ifExist(dp)) {
                DataParkir.getConnection().rubahDataParkir(dp);
                button.doClick();
                JOptionPane.showMessageDialog(frame, "Tekan ok untuk membuka palang pintu parkir");
                field.setEditable(false);
            } else {
                JOptionPane.showMessageDialog(frame, "Data kendaraan masukan tidak terdaftar");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(frame, "Data kendaraan masukan tidak terdaftar");
        }
    }
}
